﻿using System;
using Contrib.Quantcast.Services;
using Orchard.Logging;
using Orchard.Recipes.Models;
using Orchard.Recipes.Services;

namespace Contrib.Quantcast.ImportExport
{
    public class QuantcastRecipeHandler : IRecipeHandler
    {
        private readonly ISettingsService _settingsService;

        public QuantcastRecipeHandler(ISettingsService settingsService)
        {
            _settingsService = settingsService;
            Logger = NullLogger.Instance;
        }

        public ILogger Logger { get; set; }

        public void ExecuteRecipeStep(RecipeContext recipeContext)
        {
            if (!String.Equals(recipeContext.RecipeStep.Name, QuantcastCustomExportStep.ExportStep, StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var stepElement = recipeContext.RecipeStep.Step;
            
            var enable = Convert.ToBoolean(stepElement.Attribute("Enabled").Value);
            _settingsService.Set(enable, stepElement.Value);

            recipeContext.Executed = true;
        }
    }
}
